package com.lechuang.ssgou.view.activity.home;


import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.lechuang.ssgou.R;
import com.lechuang.ssgou.base.BaseFragment;
import com.lechuang.ssgou.model.bean.HomeBannerBean;
import com.lechuang.ssgou.model.bean.HomeGunDongTextBean;
import com.lechuang.ssgou.model.bean.HomeKindBean;
import com.lechuang.ssgou.model.bean.HomeLastProgramBean;
import com.lechuang.ssgou.model.bean.HomeProgramBean;
import com.lechuang.ssgou.model.bean.HomeSearchResultBean;
import com.lechuang.ssgou.presenter.CommonAdapter;
import com.lechuang.ssgou.presenter.adapter.BannerAdapter;
import com.lechuang.ssgou.presenter.adapter.HomeRecyclerAdapter;
import com.lechuang.ssgou.presenter.net.Netword;
import com.lechuang.ssgou.presenter.net.ResultBack;
import com.lechuang.ssgou.presenter.net.netApi.HomeApi;
import com.lechuang.ssgou.utils.Utils;
import com.lechuang.ssgou.view.activity.SigneActivity;
import com.lechuang.ssgou.view.activity.live.LiveActivity;
import com.lechuang.ssgou.view.activity.own.TaskCenterActivity;
import com.lechuang.ssgou.view.activity.ui.LoginActivity;
import com.lechuang.ssgou.view.defineView.AutoTextView;
import com.lechuang.ssgou.view.defineView.MGridView;
import com.lechuang.ssgou.view.defineView.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.lechuang.ssgou.utils.DesignViewUtils.isSlideToBottom;

/**
 * 作者：li on 2017/9/21 17:46
 * 邮箱：961567115@qq.com
 * 修改备注:
 */
public class HomeFragment extends BaseFragment implements AppBarLayout.OnOffsetChangedListener {

    @BindView(R.id.tv_search)
    TextView tvSearch;         //搜索
    @BindView(R.id.tv_sign)
    ImageView tvSign;           //签到
    @BindView(R.id.gv_kind)
    MGridView gvKind;        //首页分类
    Unbinder unbinder;
    @BindView(R.id.iv_top)
    ImageView ivTop;        //回到顶部
    /* @BindView(R.id.gv_product)
     MGridView gvProduct;   */ //首页最下商品gridview
    @BindView(R.id.tv_auto_text)
    AutoTextView tvAutoText; //自动滚动的textview
    @BindView(R.id.iv_program1)
    ImageView ivProgram1;
    @BindView(R.id.iv_program2)
    ImageView ivProgram2;
    @BindView(R.id.iv_program3)
    ImageView ivProgram3;
    @BindView(R.id.iv_program4)
    ImageView ivProgram4;
    @BindView(R.id.ll_noNet)
    LinearLayout llNoNet; //没有网络
    @BindView(R.id.iv_tryAgain)
    ImageView tryAgain;
    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.rv_home)
    RecyclerView rvHome;
    @BindView(R.id.renwu)
    ImageView renwu;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.root_layout)
    SwipeRefreshLayout rootLayout;
    @BindView(R.id.gv_item)
    MGridView gvItem;
    @BindView(R.id.iv_item)
    ImageView ivItem;
    @BindView(R.id.ll_gd)
    LinearLayout llGd;
    @BindView(R.id.ns_scroll)
    NestedScrollView nsScroll;
    private View v;
    private View contentView;
    private int scrollY = 0;// 标记上次滑动位置
    private ArrayList<String> text = null;
    //轮播图
    private RollPagerView mRollViewPager;
    //最下栏目广告图
    private ImageView lastRollViewPager;
    private int postion;  //顶部当前选中的位置


    //刷新重试
    //private ImageView tryAgain;

    private int images[] = {R.drawable.icon_gaunggang, R.drawable.icon_9, R.drawable.icon_1,
            R.drawable.icon_youxuan, R.drawable.icon_live};
    private String str[] = {"官方推荐", "9.9包邮", "1元试用", "品牌优选", "直播间"};
    private LinearLayoutManager linearLayoutManager;
    private List<HomeKindBean.ListBean> itemBeen;
    private int itemHeight1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_home2, container, false);
        unbinder = ButterKnife.bind(this, v);
        initView();
        initEvent();
        getData();
        sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
        return v;
    }

    //网络获取数据
    private void getData() {
        //showWaitDialog("");
        if (Utils.isNetworkAvailable(getActivity())) {
            llNoNet.setVisibility(View.GONE);
            //获取首页轮播图数据
            getHomeBannerData();
            //首页分类数据
            getHomeKindData();
            //首页滚动文字数据
            getHomeScrollTextView();
            //首页4个图片栏目数据
            getHomeProgram();
            //底部图片
            getLastImage();
            //首页最下栏目
            getLastProgramData();
            initRecyclerData();
            //lastData();
        } else {
            //隐藏加载框
            hideWaitDialog();
            llNoNet.setVisibility(View.VISIBLE);//刷新重试
            rootLayout.setVisibility(View.GONE);
            showShortToast(getString(R.string.net_error));
        }



    }

    public boolean isBottom = false;

    //滑动布局
    private void initView() {
        //轮播图
        mRollViewPager = (RollPagerView) v.findViewById(R.id.rv_banner);
        lastRollViewPager = (ImageView) v.findViewById(R.id.lastRollViewPager);

        ivItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llGd.getVisibility() == View.GONE) {
                    llGd.setVisibility(View.VISIBLE);
                } else {
                    llGd.setVisibility(View.GONE);
                }
            }
        });
        nsScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (!isBottom && scrollY+v.getHeight()>((page-1)*10+9)*250 && scrollY > oldScrollY) {
                    isBottom = true;
                    page += 1;
                    initRecyclerData();
                    showWaitDialog("");
                }

                if(scrollY<200&&ivTop.getVisibility()==View.VISIBLE){
                    ivTop.setVisibility(View.GONE);
                }else if(scrollY>200) {
                    ivTop.setVisibility(View.VISIBLE);
                }

            }
        });

        rvHome.addOnScrollListener(new SwipyAppBarScrollListener(appBarLayout,rootLayout, rvHome));
        if (appBarLayout != null) {
            appBarLayout.addOnOffsetChangedListener(this);
        }

        rootLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
    }

    boolean result;
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (rootLayout == null)
            return;
        boolean tem = isSlideToBottom(rvHome);
        result = verticalOffset >= 0 || tem ? true : false;
        appBarLayout.setEnabled(result);

//        if (isSlideToBottom(mRecycler)) {
//            Log.i("onOffsetChanged"," ----- "+isSlideToBottom(mRecycler));
//        }
    }

    //置顶按钮
    private void initEvent() {
        //tryAgain = (ImageView) v.findViewById(R.id.tryAgain);

    }

    private List<HomeBannerBean.IndexBannerList> bannerList;

    //获取首页轮播图数据
    private void getHomeBannerData() {
        //首页轮播图数据
        Netword.getInstance().getApi(HomeApi.class)
                .homeBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<HomeBannerBean>(getActivity()) {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideWaitDialog();
                    }

                    @Override
                    public void successed(HomeBannerBean result) {
                        if (result == null) {
                            return;
                        }
                        bannerList = result.indexBannerList0;
                        List<String> imgList = new ArrayList<>();
                        for (int i = 0; i < bannerList.size(); i++) {
                            imgList.add(bannerList.get(i).img);
                        }
                        //设置播放时间间隔
                        mRollViewPager.setPlayDelay(3000);
                        //设置透明度
                        mRollViewPager.setAnimationDurtion(500);
                        //设置适配器
                        mRollViewPager.setAdapter(new BannerAdapter(getActivity(), imgList));
                        //设置指示器（顺序依次）
                        //自定义指示器图片
                        //设置圆点指示器颜色
                        //设置文字指示器
                        //隐藏指示器
                        //mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
                        mRollViewPager.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW, Color.WHITE));
                        //mRollViewPager.setHintView(new TextHintView(this));
                        //mRollViewPager.setHintView(null);
                        mRollViewPager.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(int position) {
                                //获取到点击条目
                                int programaId = bannerList.get(position).programaId;
                                if (programaId == 1) {
                                    //栏目1
                                    startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                            .putExtra("programaId", 1));
                                } else if (programaId == 2) {
                                    //栏目2
                                    startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                            .putExtra("programaId", 2));
                                } else if (programaId == 3) {
                                    //栏目3
                                    startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                            .putExtra("programaId", 3));
                                } else if (programaId == 4) {
                                    //栏目4
                                    startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                            .putExtra("programaId", 4));
                                } else if (programaId == 5) {

                                } else {
                                    // TODO: 2017/10/1 跳转奔溃
                                    startActivity(new Intent(getActivity(), EmptyWebActivity.class)
                                            .putExtra("url", bannerList.get(position).link));
                                }
                            }
                        });
                    }
                });
    }

    //获取首页分类数据
    private void getHomeKindData() {

        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("image", images[i]);
            map.put("name", str[i]);
            list.add(map);
        }
        gvKind.setAdapter(new CommonAdapter<Map<String, Object>>(getActivity(), list, R.layout.home_kinds_item1) {
            @Override
            public void setData(ViewHolder viewHolder, Object item) {
                viewHolder.getView(R.id.iv_kinds_img).setVisibility(View.GONE);
                viewHolder.getView(R.id.iv_own).setVisibility(View.VISIBLE);
                HashMap<String, Object> map = (HashMap<String, Object>) item;
                String name = (String) map.get("name");
                int img = (int) map.get("image");
                viewHolder.setText(R.id.tv_kinds_name, name);
                viewHolder.setImageResource(R.id.iv_own, img);
            }
        });
        gvKind.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(getActivity(), KindDetailActivity.class)
                                .putExtra("type", 1).putExtra("name", "官方推荐"));
                        break;
                    case 1:
                        startActivity(new Intent(getActivity(), KindDetailActivity.class)
                                .putExtra("type", 2).putExtra("name", "9.9包邮"));
                        break;
                    case 2:
                        startActivity(new Intent(getActivity(), KindDetailActivity.class)
                                .putExtra("type", 3).putExtra("name", "1元试用"));
                        break;
                    case 3:
                        startActivity(new Intent(getActivity(), KindDetailActivity.class)
                                .putExtra("type", 4).putExtra("name", "品牌优选"));
                        break;
                    case 4:
                        startActivity(new Intent(getActivity(), LiveActivity.class));


                        break;
                }
            }
        });


    }

    //首页滚动文字数据
    private void getHomeScrollTextView() {
        Netword.getInstance().getApi(HomeApi.class)
                .gunDongText()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<HomeGunDongTextBean>(getActivity()) {
                               @Override
                               public void successed(HomeGunDongTextBean result) {
                                   if (result == null) {
                                       return;
                                   }
                                   final List<HomeGunDongTextBean.IndexMsgListBean> list = result.indexMsgList;
                                   //滚动TextView
                                   text = new ArrayList<>();
                                   for (int i = 0; i < list.size(); i++) {
                                       text.add(list.get(i).name);
                                   }
                                   //自定义的滚动textview
                                   tvAutoText.setTextAuto(text);
                                   //设置时间
                                   tvAutoText.setGap(3000);
                                   tvAutoText.setOnItemClickListener(new AutoTextView.onItemClickListener() {
                                       @Override
                                       public void onItemClick(int position) {
                                           if (sp.getBoolean("isLogin", false)) {
                                               startActivity(new Intent(getActivity(), ProductDetailsActivity.class)
                                                       .putExtra("alipayCouponId", list.get(position).alipayCouponId)
                                                       .putExtra("alipayItemId", list.get(position).alipayItemId)
                                                       .putExtra("name", list.get(position).name)
                                                       .putExtra("price", list.get(position).price + "")
                                                       .putExtra("preferentialPrice", list.get(position).preferentialPrice)
                                                       .putExtra("shareIntegral", list.get(position).shareIntegral)
                                                       .putExtra("shopType", list.get(position).shopType)
                                                       .putExtra("smallImages", list.get(position).smallImages)
                                                       .putExtra("img", list.get(position).imgs));
                                           } else {
                                               startActivity(new Intent(getActivity(), LoginActivity.class));
                                           }
                                       }
                                   });
                               }
                           }
                );
    }

    //首页4个图片栏目数据
    private void getHomeProgram() {
        Netword.getInstance().getApi(HomeApi.class)
                .homeProgramaImg()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<HomeProgramBean>(getActivity()) {
                    @Override
                    public void successed(HomeProgramBean result) {
                        if (result == null) {
                            return;
                        }
                        final List<HomeProgramBean.ListBean> list = result.programaImgList;
                        List<String> imgList = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            if (list.get(i) != null)
                                imgList.add(list.get(i).img);
                        }
                        //栏目1
                        if (imgList.get(0) != null)
                            Glide.with(getActivity()).load(imgList.get(0)).into(ivProgram1);
                        //栏目2
                        if (imgList.get(1) != null)
                            Glide.with(getActivity()).load(imgList.get(1)).into(ivProgram2);
                        //栏目3
                        if (imgList.get(2) != null)
                            Glide.with(getActivity()).load(imgList.get(2)).into(ivProgram3);
                        //栏目4
                        if (imgList.get(3) != null)
                            Glide.with(getActivity()).load(imgList.get(3)).into(ivProgram4);
                        ivProgram1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                        .putExtra("programaId", list.get(0).programaId));
                            }
                        });
                        ivProgram2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                        .putExtra("programaId", list.get(1).programaId));
                            }
                        });
                        ivProgram3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                        .putExtra("programaId", list.get(2).programaId));
                            }
                        });
                        ivProgram4.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(getActivity(), ProgramDetailActivity.class)
                                        .putExtra("programaId", list.get(3).programaId));
                            }
                        });
                    }
                });
    }

    //获取最底部图片
    private void getLastImage() {
        //底部图片
        Netword.getInstance().getApi(HomeApi.class)
                .homeLastPage(page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<HomeLastProgramBean>(getActivity()) {
                    @Override
                    public void successed(HomeLastProgramBean result) {
                        if (result == null)
                            return;
                        List<HomeLastProgramBean.programaBean.ListBean> list1 = result.programa.indexBannerList;

                        Glide.with(getActivity()).load(list1.get(0).img).into(lastRollViewPager);
                    }

                });
    }

    //首页最下栏目的数据
    private void getLastProgramData() {

        Netword.getInstance().getApi(HomeApi.class)
                .homeClassify()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<HomeKindBean>(getActivity()) {
                    @Override
                    public void successed(HomeKindBean result) {
                        if (result == null) {
                            return;
                        }
                        List<HomeKindBean.ListBean> list = result.tbclassTypeList;
                        //取前10类
                        if (list.size() > 10) {
                            topTabList.clear();
                            list = list.subList(0, 10);
                        }
                        HomeKindBean.ListBean topTab = new HomeKindBean.ListBean();
                        topTab.rootId = -1;
                        topTab.rootName = "精选";
                        topTabList.add(topTab);
                        List<HomeKindBean.ListBean> tabList = list;
                        itemBeen = tabList.subList(0, 10);
                        if (tabList != null) {
                            topTabList.addAll(tabList);
                        }
//                        initFragment();
                        initTabView();
                        //initGride();
                        gvItem.setAdapter(new CommonAdapter<HomeKindBean.ListBean>(getActivity(), itemBeen, R.layout.home_kinds_item) {
                            @Override
                            public void setData(ViewHolder viewHolder, Object item) {
                                HomeKindBean.ListBean bean = (HomeKindBean.ListBean) item;
                                //分类名称
                                viewHolder.setText(R.id.tv_kinds_name, bean.rootName);
                                //分类图片
                                viewHolder.displayImage(R.id.iv_kinds_img, bean.img);
                            }
                        });
                        // TODO: 2017/10/2  分类item
                        gvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                rootId = topTabList.get(HomeFragment.this.postion + 1).rootId;
                                page = 1;
                                initRecyclerData();
                                llGd.setVisibility(View.GONE);
                            }
                        });

                    }
                });
    }


    /**
     * 初始化tablayout
     */
    private void initTabView() {
        initRecyclerData();
        for (int i = 0; i < topTabList.size(); i++) {
            TabLayout.Tab tab = tlTab.newTab();
            tab.setText(topTabList.get(i).rootName).setTag(i);
            tlTab.addTab(tab);
        }
        //设置tablout 滑动模式
        tlTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        tlTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //记录当前位置
                postion = (int) tab.getTag();
                rootId = topTabList.get(HomeFragment.this.postion).rootId;
                page = 1;
                initRecyclerData();

                //ToastManager.getInstance().showShortToast("onTabSelected" + postion);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    HashMap<String, String> map = new HashMap<>();
    SharedPreferences sp;
    //tablayoute标题
    private List<HomeKindBean.ListBean> topTabList = new ArrayList<>();
    private int rootId = -1;
    //底部数据展示
    private HomeRecyclerAdapter mAdapter;
    private List<HomeSearchResultBean.ProductListBean> productList = new ArrayList<>();

    /**
     * 初始化底部数据
     */
    private void initRecyclerData() {
        showWaitDialog("");
        map.clear();
        map.put("page", page + "");
        if (rootId != -1) {  //Integer classTypeId   分类id,精选不传
            map.put("classTypeId", rootId + "");
        } else {
            if (map.containsKey("classTypeId")) {
                map.remove("classTypeId");
            }
            //Integer is_official   精选传1,其他不传
            map.put("is_official", 1 + "");
        }
        Netword.getInstance().getApi(HomeApi.class)
                .searchResult(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<HomeSearchResultBean>(getActivity()) {
                    @Override
                    public void successed(HomeSearchResultBean result) {
                        if (result == null)
                            return;
                        List<HomeSearchResultBean.ProductListBean> list = result.productList;

                        if (page == 1) {
                            productList.clear();
                        }
                        productList.addAll(list);
                        if (page > 1 && list.isEmpty()) {
                            Utils.show(getActivity(), "亲,已经到底了~");
                            return;
                        }
                        int isAgencyStatus = sp.getInt("isAgencyStatus", 0);

                        if (mAdapter == null) {
                            mAdapter = new HomeRecyclerAdapter(productList, getContext(), isAgencyStatus);
                            linearLayoutManager = new LinearLayoutManager(getContext());
                            linearLayoutManager.setSmoothScrollbarEnabled(true);
                           /* itemHeight = linearLayoutManager.getChildAt(1).getHeight();*/
                           /* rvHome.setHasFixedSize(true);*/
                            rvHome.setNestedScrollingEnabled(false);
                            rvHome.setLayoutManager(linearLayoutManager);
                            rvHome.setAdapter(mAdapter);
                            mAdapter.setOnItemClickLitener(new HomeRecyclerAdapter.OnItemClickLitener() {
                                @Override
                                public void onItemClick(View view, int position) {
                                    startActivity(new Intent(getActivity(), ProductDetailsActivity.class)
                                            .putExtra("alipayCouponId", productList.get(position).alipayCouponId)
                                            .putExtra("alipayItemId", productList.get(position).alipayItemId)
                                            .putExtra("name", productList.get(position).name)
                                            .putExtra("price", productList.get(position).price + "")
                                            .putExtra("preferentialPrice", productList.get(position).preferentialPrice)
                                            .putExtra("shareIntegral", productList.get(position).shareIntegral)
                                            .putExtra("shopType", productList.get(position).shopType)
                                            .putExtra("smallImages", productList.get(position).smallImages)
                                            .putExtra("img", productList.get(position).imgs));
                                }
                            });

                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        isBottom = false;
                        hideWaitDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideWaitDialog();
                    }
                });
    }


    @OnClick({R.id.tv_search, R.id.tv_sign,R.id.iv_top, R.id.renwu, R.id.iv_tryAgain})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.renwu:
                startActivity(new Intent(getActivity(), TaskCenterActivity.class));
                //搜索
                break;
            case R.id.iv_top:
                nsScroll.smoothScrollTo(0,0);
                ivTop.setVisibility(View.GONE);
                //搜索
                break;
            case R.id.tv_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            //签到
            case R.id.tv_sign:
                if (sp.getBoolean("isLogin", false) == true) {
                    startActivity(new Intent(getActivity(), SigneActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            //底部跳转到顶部的按钮
         /*   case R.id.iv_top:
                scrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        //  scrollView.fullScroll(ScrollView.FOCUS_DOWN);滚动到底部
                        //  scrollView.fullScroll(ScrollView.FOCUS_UP);滚动到顶部
                        //  需要注意的是，该方法不能直接被调用
                        //  因为Android很多函数都是基于消息队列来同步，所以需要异步操作，
                        //  addView完之后，不等于马上就会显示，而是在队列中等待处理，如果立即调用fullScroll， view可能还没有显示出来，所以会失败
                        //  应该通过handler在新线程中更新
                        scrollView.fullScroll(ScrollView.FOCUS_UP);
                    }
                });
                ivTop.setVisibility(View.GONE);
                break;*/
            case R.id.iv_tryAgain:
                showWaitDialog("");
                //刷新数据
                page = 1;
                getData();
                break;
            default:
                break;
        }
    }


    //分页
    private int page = 1;


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}

